#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include "client.h"

enum def_conf_type { RED, GREEN, BLUE };

struct address_info {
	std::string name = "";
	std::string address = "";
};

typedef struct tag_config_info
{
	std::string outbound_ip = "";
	std::string outbound_name = "";
	std::string inbound_ip = "";
	std::string inbound_name = "";
	address_info addr_info;
	def_conf_type conf_type = def_conf_type::GREEN;
	int listen_port = 0;
	int tx_port = 0;
} config_info;

std::ostream& operator<<(std::ostream& os, const def_conf_type& c) {
	switch (c) {
	case def_conf_type::RED: os << "RED"; break;
	case def_conf_type::GREEN: os << "GREEN"; break;
	case def_conf_type::BLUE: os << "BLUE"; break;
	}
	return os;
}

std::istream& operator>>(std::istream& is, def_conf_type& c) {
	std::string str;
	is >> str;
	if (str == "RED") {
		c = def_conf_type::RED;
	}
	else if (str == "GREEN") {
		c = def_conf_type::GREEN;
	}
	else if (str == "BLUE") {
		c = def_conf_type::BLUE;
	}
	else {
		is.setstate(std::ios::failbit);
	}
	return is;
}

int client()
{
	config_info config;
	config.outbound_ip = "127.0.0.1";
	config.outbound_name = "127.0.0.1";
	config.inbound_ip = "192.168.1.1";
	config.inbound_name = "127.0.0.1";
	config.addr_info.name = "ABC";
	config.addr_info.address = "ABC";
	config.listen_port = 1234;
	config.tx_port = 5678;

	std::stringstream ss;
	ss << config.outbound_ip << " ";
	ss << config.outbound_name << " ";
	ss << config.inbound_ip << " ";
	ss << config.inbound_name << " ";
	ss << config.addr_info.name << " ";
	ss << config.addr_info.address << " ";
	ss << config.conf_type << " ";
	ss << config.listen_port << " ";
	ss << config.tx_port << " ";

	std::string serialized_data = ss.str();
	const char* data = serialized_data.c_str();
	size_t data_size = serialized_data.size();

	int sock = socket(AF_INET, SOCK_STREAM, 0);
	struct sockaddr_in server_addr;
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(12345);
	server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

	connect(sock, (struct sockaddr*)&server_addr, sizeof(server_addr));
	send(sock, data, data_size, 0);

	close(sock);

	return 0;
}
